package fem2.gui;

import fem2.Model;
import inf.jlinalg.ArrayVector;
import inf.swing.IntTextField;
import inf.vtk.pm.Pipeline;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ShowDofsDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private class ApplyJob {

		public void doWork(Pipeline pipeline) {
			int n;
			IntTextField mtf = getModeTF();
			ArrayVector u = new ArrayVector();
			ModelHolder mh = (ModelHolder) pipeline.getModel();
			Model model = mh.getModel();

			// set a random global solution vector
			model.preAnalysis();
			n = model.getMesh().countDofs();
			mtf.setRange(0, n - 1);
			mtf.setIntValue(Math.min(mtf.getIntValue(), n - 1));

			u.setSize(n);
			u.set(mtf.getIntValue(), 1.0);
			// model.postAnalysis(u.getValues());

			mh.solvex();
			pipeline.render();
		}
	}

	private JPanel centerPanel_;
	private JPanel southPanel_;
	private JPanel modePanel_;
	private IntTextField modeTF_;
	private JButton applyB_;
	private JButton closeB_;
	private Pipeline pipeline_;

	public ShowDofsDialog(Frame owner, Pipeline pipeline) {
		super(owner);

		pipeline_ = pipeline;
		initialize();
	}

	private void apply() {
		new ApplyJob().doWork(pipeline_);
	}

	private JButton getApplyB() {
		if (applyB_ == null) {
			applyB_ = new JButton("Apply");
			applyB_.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					apply();
				}
			});
		}
		return applyB_;
	}

	private JPanel getCenterPanel() {
		if (centerPanel_ == null) {
			centerPanel_ = new JPanel(new BorderLayout());
			centerPanel_.add(getModePanel(), BorderLayout.NORTH);
		}
		return centerPanel_;
	}

	private JButton getCloseB() {
		if (closeB_ == null) {
			closeB_ = new JButton("Close");

			closeB_.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return closeB_;
	}

	private JPanel getModePanel() {
		if (modePanel_ == null) {
			modePanel_ = new JPanel(new BorderLayout(5, 5));
			modePanel_.add(new JLabel("Select DOF"), BorderLayout.WEST);
			modePanel_.add(getModeTF());
		}
		return modePanel_;
	}

	private IntTextField getModeTF() {
		if (modeTF_ == null) {
			modeTF_ = new IntTextField(0);
			modeTF_.addChangeListener(new ChangeListener() {

				@Override
				public void stateChanged(ChangeEvent e) {
					apply();
				}
			});
			modeTF_.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					apply();
				}
			});
		}
		return modeTF_;
	}

	private JPanel getSouthPanel() {
		if (southPanel_ == null) {
			southPanel_ = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			southPanel_.add(getApplyB());
			southPanel_.add(getCloseB());
		}
		return southPanel_;
	}

	private void initialize() {
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		((JPanel) getContentPane()).setBorder(BorderFactory.createLineBorder(getBackground(), 5));
		add(getCenterPanel());
		add(new JSeparator());
		add(getSouthPanel());
		pack();
	}
}
